package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SpuManageServiceImpl implements SpuManageService {
    @Autowired
    private SpuImageService spuImageService;
    @Autowired
    private SpuInfoService spuInfoService;
    @Autowired
    private SpuPosterService spuPosterService;
    @Autowired
    private SpuSaleAttrService spuSaleAttrService;
    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;
    @Autowired
    private BaseSaleAttrService baseSaleAttrService;
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    /**
     * 分页查询商品Spu列表
     *
     * @param iPage
     * @param category3Id
     * @return
     */
    @Override
    public IPage<SpuInfo> getSpuListByPage(IPage<SpuInfo> iPage, Long category3Id) {
        LambdaQueryWrapper<SpuInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuInfo::getCategory3Id,category3Id);
        //排序
        queryWrapper.orderByDesc(SpuInfo::getUpdateTime);
        IPage<SpuInfo> iPage1 = spuInfoService.page(iPage, queryWrapper);
        return iPage1;
    }

    /**
     * 查询平台所有的销售属性名称
     *
     * @return
     */
    @Override
    public List<BaseSaleAttr> getBaseSaleAttrList() {
        List<BaseSaleAttr> list = baseSaleAttrService.list();
        return list;
    }

    /**
     * 保存商品SPU信息
     * 1.将商品基本信息封装SpuInfo对象 存入spu_info表
     * 2.将提交商品图片封装到SpuImage集合中 存入spu_image表
     * 3.将提交海报图片封装到SpuPost集合中 存入spu_poster表
     * 4.将提交销售属性名称SpuSaleAttr集合 存入spu_sale_attr表
     * 5.将提交销售属性值SpuSaleAttrValue集合 存入spu_sale_attr_value表
     *
     * @param spuInfo
     * @return
     */
    @Override
    //添加事务
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        spuInfoService.save(spuInfo);
/*        spuImageService.saveBatch(spuInfo.getSpuImageList());
        spuPosterService.saveBatch(spuInfo.getSpuPosterList());
        spuSaleAttrService.saveBatch(spuInfo.getSpuSaleAttrList());*/
        Long id=spuInfo.getId();
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)){
            spuImageList.stream().forEach(spuImage ->{
                spuImage.setSpuId(id);
            });
            //批量保存
            spuImageService.saveBatch(spuImageList);

        }
        //同理
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)){
            spuPosterList.stream().forEach(spuPoster ->{
                spuPoster.setSpuId(id);
            });
            //批量保存
            spuPosterService.saveBatch(spuPosterList);
        }
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)){
            spuSaleAttrList.stream().forEach(spuSaleAttr->{
                spuSaleAttr.setSpuId(id);
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)){
                    spuSaleAttrValueList.stream().forEach(spuSaleAttrValue ->{
                        spuSaleAttrValue.setSpuId(id);
                        //为平台属性名称赋值
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                    });
                    spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
                }

            });
            spuSaleAttrService.saveBatch(spuSaleAttrList);
        }

    }

    /**
     * 根据SPUID查询商品销售属性列表(包含属性值)
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        List<SpuSaleAttr> list = spuSaleAttrMapper.getSpuSaleAttrList(spuId);
        return null;
    }

    /**
     * 根据SPUID查询商品销售属性列表(包含属性值)
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuImage> getSpuImageList(Long spuId) {
        LambdaQueryWrapper<SpuImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuImage::getSpuId,spuId);
        List<SpuImage> list = spuImageService.list(queryWrapper);
        return list;
    }

    /**
     * 根据spuId 获取海报数据
     *
     * @param spuId
     * @return
     */
    @Override
    @GmallCache(prefix = "spuPoster:")
    public List<SpuPoster> getSpuPosterBySpuId(Long spuId) {
        LambdaQueryWrapper<SpuPoster> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuPoster::getSpuId,spuId);
        List<SpuPoster> list = spuPosterService.list(queryWrapper);
        return list;
    }

    /**
     * 查询spu所有销售属性,将指定传入sku商品销售属性区分是否选中
     *
     * @param skuId
     * @param spuId
     * @return
     */
    @Override
    @GmallCache(prefix = "spuSaleAttrList:")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {
        List<SpuSaleAttr> list= spuSaleAttrMapper.getSpuSaleAttrListCheckBySku( skuId,  spuId);
        return list;
    }
}
